/*
 * Classname Principal.java
 * 
 * Version Information
 * Date Mar 20, 2006
 *
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.isso.hibernate;

import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;

import net.amsoft.iservice.util.exception.IServiceException;

/**
 * @author jyoti
 * 
 */
public class Principal extends ISSOObject {
    private static Logger logger = Logger.getLogger(Principal.class);

    private IBroker broker;
    private Set<Synonym> synonyms = new HashSet<Synonym>();
    private Set<PrincipalMetaData> principalMetaData = new HashSet<PrincipalMetaData>();
    private Set<Cookie> cookie = new HashSet<Cookie>();
    private String sUnEncryptedGreeting;
    public Principal() {
    }

    public IBroker getBroker() {
        return this.broker;
    }
    public void setBroker(IBroker broker) {
        this.broker = broker;
    }

    public Set getSynonyms() {
        return this.synonyms;
    }
    public void setSynonyms(Set<Synonym> synonyms) {
        this.synonyms = synonyms;
    }

    public Set getPrincipalMetaData() {
        return this.principalMetaData;
    }
    public void setPrincipalMetaData(Set<PrincipalMetaData> principalMetaData) {
        this.principalMetaData = principalMetaData;
    }

    public Set getCookie() {
        return this.cookie;
    }
    public void setCookie(Set<Cookie> cookie) {
        this.cookie = cookie;
    }

    public String getUnEncryptedGreeting() {
        return this.sUnEncryptedGreeting;
    }
    public void setUnEncryptedGreeting(String sUnEncryptedGreeting) {
        this.sUnEncryptedGreeting = sUnEncryptedGreeting;
    }

    public void addSynonym(Synonym synonym) throws IServiceException {
        synonym.setPrincipal(this);
        this.synonyms.add(synonym);
        try {
            synonym.insert();
        } catch (IServiceException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public void removeSynonym(Synonym synonym) {
        synonym.remove();
    }

    public void addCookie(Cookie cookie) throws IServiceException {
        cookie.setPrincipal(this);
        this.cookie.add(cookie);
        cookie.insert();
    }

    public void removeCookie(Cookie cookie) {
        this.cookie.remove(cookie);
        cookie.remove();
    }

    public void addPrincipalMetaData(PrincipalMetaData data)
            throws IServiceException {
        data.setPrincipal(this);
        this.principalMetaData.add(data);
        data.insert();
    }

    public void removePrincipalMetaData(PrincipalMetaData data) {
        this.principalMetaData.remove(data);
        data.remove();
    }

    public void debug(String prefix) {
        this.update();
        logger.debug(prefix + " : Subscriber (" + id + ", " + broker.getId()
                + ", " + creationTime + ", " + modificationTime + ", "
                + synonyms.size() + ", " + state + ")");
    }
}
